/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.Start;
import java.io.File;
import java.util.ArrayList;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import model.AmountOfIngredient;
import model.Ingredient;
import model.Recipe;

/**
 *
 * @author Jonas
 */
public class AddRecipePanel extends javax.swing.JPanel {

    /**
     * Creates new form addRecipePanel
     */
    
    ArrayList<AmountOfIngredient> aoingList;
    
    public AddRecipePanel() {
	aoingList = new ArrayList<>();
	initComponents();
	for (Ingredient ing : Start.ih.getIngredientList()) {	    
	    jInName.addItem((Ingredient)ing);
	}
	setSize(600, 300);
    }
    
    private void addIngToList(){
	jInList.setText("");
	for (AmountOfIngredient a : aoingList) {
	    jInList.append(a.getIng().getName()+":"+a.getAmount()+a.getIng().getUnit()+"; ");
	}
	jReCookingTime.setText((aoingList.size()*5)+"");
    }
    
    private void addRecipe(){
	String path = jReBillede.getText();
	Recipe re = new Recipe(jReName.getText(), path, jReDesc.getText(), jReHowTo.getText(), Integer.parseInt(jReCookingTime.getText()),(ArrayList<AmountOfIngredient>) aoingList.clone());
	Start.rh.addRecipe(re);
	jFeedbackLabel.setText(jReName.getText()+" blev tilføjet!");
	clearAddPage();
    }
    
    private void clearAddPage(){
	aoingList.clear();
	jReName.setText("");
	jReBillede.setText("");
	jInName.setSelectedIndex(0);
	jInUnit.setText("");
	jInList.setText("");
	jReDesc.setText("");
	jReHowTo.setText("");
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        jLabel1 = new javax.swing.JLabel();
        jReName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jReCookingTime = new javax.swing.JTextField();
        jReBillede = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jInName = new javax.swing.JComboBox();
        jInUnit = new javax.swing.JTextField();
        jInUnitLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jInList = new javax.swing.JTextArea();
        jInAdd = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jReDesc = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jReHowTo = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jInUnitLabel1 = new javax.swing.JLabel();
        jFeedbackLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(197, 0, 7));

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Navn:");

        jReName.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jReName.setBorder(null);

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Tilberedningstid:");

        jReCookingTime.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jReCookingTime.setBorder(null);

        jReBillede.setEditable(false);
        jReBillede.setBackground(new java.awt.Color(255, 255, 255));
        jReBillede.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jReBillede.setBorder(null);
        jReBillede.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jReBilledeMouseClicked(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Billede:");

        jButton1.setBackground(new java.awt.Color(200, 200, 200));
        jButton1.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton1.setText("Tilføj opskrift");
        jButton1.setToolTipText("");
        jButton1.setBorder(null);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Ingredienser:");

        jInName.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jInName.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jInNameItemStateChanged(evt);
            }
        });

        jInUnit.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jInUnit.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jInUnit.setBorder(null);

        jInUnitLabel.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jInUnitLabel.setForeground(new java.awt.Color(255, 255, 255));
        jInUnitLabel.setText("unit");

        jInList.setEditable(false);
        jInList.setColumns(20);
        jInList.setFont(new java.awt.Font("Monospaced", 0, 11)); // NOI18N
        jInList.setLineWrap(true);
        jInList.setRows(5);
        jInList.setBorder(null);
        jScrollPane1.setViewportView(jInList);

        jInAdd.setBackground(new java.awt.Color(200, 200, 200));
        jInAdd.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jInAdd.setText("Tilføj Ingrediens");
        jInAdd.setToolTipText("");
        jInAdd.setBorder(null);
        jInAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jInAddActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Verdana", 1, 24)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Tilføj Opskrift");

        jReDesc.setColumns(20);
        jReDesc.setRows(5);
        jScrollPane2.setViewportView(jReDesc);

        jReHowTo.setColumns(20);
        jReHowTo.setRows(5);
        jScrollPane3.setViewportView(jReHowTo);

        jLabel6.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Beskrivelse:");

        jLabel7.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Tilberedning:");

        jInUnitLabel1.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jInUnitLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jInUnitLabel1.setText("minutter");

        jFeedbackLabel.setFont(new java.awt.Font("Verdana", 1, 20)); // NOI18N
        jFeedbackLabel.setForeground(new java.awt.Color(230, 180, 0));
        jFeedbackLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jFeedbackLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jReCookingTime, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jInUnitLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel7))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jReName)
                                    .addComponent(jInName, 0, 224, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jInUnit)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jInUnitLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jInAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                                        .addComponent(jReBillede, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(jScrollPane2)
                            .addComponent(jScrollPane3))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jReName, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jReBillede, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jInName, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jInAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jInUnitLabel)
                        .addComponent(jInUnit, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                        .addGap(7, 7, 7))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(jFeedbackLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jReCookingTime, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jInUnitLabel1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jReBilledeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jReBilledeMouseClicked
	FileFilter f = new FileNameExtensionFilter("Billede", "jpg", "jpeg", "gif", "png", "bmp");
	jFileChooser1.addChoosableFileFilter(f);
	jFileChooser1.setAcceptAllFileFilterUsed(false);
	int isFile = jFileChooser1.showDialog(this, "Vælg billede");
	System.out.println("Stuff done: " + isFile);
	if (isFile == 0) {
	    File file = jFileChooser1.getSelectedFile();
	    System.out.println(file);
	    jReBillede.setText(file.getAbsolutePath());
	}
    }//GEN-LAST:event_jReBilledeMouseClicked

    private void jInNameItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jInNameItemStateChanged
        Ingredient ing = (Ingredient) jInName.getSelectedItem();
	jInUnitLabel.setText(ing.getUnit());
	jInUnit.setText("0");
    }//GEN-LAST:event_jInNameItemStateChanged

    private void jInAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jInAddActionPerformed
        AmountOfIngredient aoi = new AmountOfIngredient(Integer.parseInt(jInUnit.getText()),(Ingredient)jInName.getSelectedItem());
	jInUnit.setText("0");
	aoingList.add(aoi);
	addIngToList();
    }//GEN-LAST:event_jInAddActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        addRecipe();
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jFeedbackLabel;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JButton jInAdd;
    private javax.swing.JTextArea jInList;
    private javax.swing.JComboBox jInName;
    private javax.swing.JTextField jInUnit;
    private javax.swing.JLabel jInUnitLabel;
    private javax.swing.JLabel jInUnitLabel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jReBillede;
    private javax.swing.JTextField jReCookingTime;
    private javax.swing.JTextArea jReDesc;
    private javax.swing.JTextArea jReHowTo;
    private javax.swing.JTextField jReName;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
}
